package com.lei.mq;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeoutException;

/**
 * 简单消息队列
 * Created by Administrator on 2019/2/26.
 */
@Component
public class S01SimpleProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Bean
    public Queue simpleQueue(){
        //队列数据默认是持久化的
        return new Queue("simple.queue");
    }
    @Scheduled(fixedDelay = 2000L)
    public void send() throws IOException, TimeoutException {
        System.out.println("S01SimpleProducer");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
        String content = format.format(new Date());
        //默认的exchannle为""，当交换机为默认的""时，simple.queue 缺省就是队列名
        //The default exchange is implicitly bound to every queue, with a routing key equal to the queue name.
        //It is not possible to explicitly bind to, or unbind from the default exchange. It also cannot be deleted.
        rabbitTemplate.convertAndSend("simple.queue","S01SimpleProducer "+content);
    }
}
